<%--
  Created by IntelliJ IDEA.
  User: 沸羊羊
  Date: 2022/11/2
  Time: 16:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>物料组成设计单</title>
    <style>
        #MaterialCompositionDesignSheetTab th{
            background-color: lightblue;
        }
    </style>
</head>
<body>
<div style="width: 1236px;height: 682px;border:red solid 0px;background: #E5F4EF">
        <span style="color: blue;">
            你正在做的业务是: 产品设计--产品档案管理--物料组成设计单
        </span>

    <div class="row" style="margin-top: 50px;margin-right: 20px;">
        <div class="col-md-offset-7 col-md-5" >
            <input type="button" onclick="addFood()" class="btn btn-primary" value="添加物料">
            <input type="button" onclick="delMaterialCompositionDesignSheet()" class="btn btn-default" value="删除物料">
            <input type="button" class="btn btn-default" value="确认" onclick="MaterialCompositionDesignSheetConfirm()">
            <input type="button" class="btn btn-default" value="返回" onclick="return2()">
        </div>
    </div>
    <form id="MaterialCompositionDesignSheetForm">
        <div class="container" style="width: 800px;height: 550px;border: black solid 1px;">
                <h2 align="center">物料组成设计单</h2>
            <br>
            <div class="row">
                <div class="col-md-offset-2 col-md-4">
                    <b>产品名称：</b>
                    <input id="MaterialCompositionDesignSheetProductName" style="outline: none;border:none;background-color: #E5F4EF;width: 150px" readonly name="productname">
                </div>
                <div class="col-md-offset-1 col-md-5">
                    <b>产品编号：</b>
                    <input id="MaterialCompositionDesignSheetProductId" style="outline: none;border:none;background-color: #E5F4EF;width: 150px"  readonly name="productid">
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-offset-2 col-md-10">
                    <b>设计人：</b>
                    <input type="text" style="outline: none;border-width: 0px 0px 1px 0px;background-color: #E5F4EF;width: 150px" id="MaterialCompositionDesignSheet" value="${username}" readonly>
                </div>
            </div>
            <br>
            <table id="MaterialCompositionDesignSheetTab">

            </table>
            <br>
            <div class="row">
                <div class="col-md-offset-2 col-md-4">
                    <b>登记人：</b>
                    <input type="text" style="outline: none;border-width: 0px 0px 1px 0px;background-color: #E5F4EF;width: 150px" id="MaterialCompositionDesignSheetRegister" value="${username}" readonly>
                </div>
                <div class="col-md-offset-1 col-md-5">
                    <b>登记时间：</b>
                    <span id="MaterialCompositionDesignSheetRegisterTime"></span>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-offset-2 col-md-10">
                    <b style="position: relative;top: -41px;">设计要求：</b>
                    <textarea id="MaterialCompositionDesignSheetModuleDescribe" style="width: 450px;height: 95px;resize: none;" name="moduledescribe"></textarea>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="modal fade  texts" data-backdrop="static" id="DesignTabModal" style="width:900px;margin-left: 500px">
    <div class="modal-dialog" style="width: 788px">
        <div class="modal-content" id="DesignTab_Modal">

        </div>
    </div>
</div>

    <script>

        //改变数量
        function MaterialCompositionDesignSheetChangeAmount(obj) {

            //获取表单里所有的数据
            var all=$("#MaterialCompositionDesignSheetTab").bootstrapTable("getData")

            //获取当前改变数量的那个产品的物料编号
            var productId=$(obj).parent().parent().find("td:eq(1)").text();
            $(all).each(function (index,item) {
                if(item.productid==productId){
                    item.amount=$(obj).val();
                    item.subtotal=$(obj).parent().next().next().text()*$(obj).val()
                    $("#MaterialCompositionDesignSheetTab").bootstrapTable("load",all)
                }
            })


        }

        //确定
        function MaterialCompositionDesignSheetConfirm() {



            //获取表单里的数据
            var module=$("#MaterialCompositionDesignSheetForm").serializeArray();
            var module2 = {};
            $(module).each(function (index,item) {
                module2[item.name]=item.value;
            })
            //获取表格的所有数据
            var obj=$("#MaterialCompositionDesignSheetTab").bootstrapTable("getData");

            for(var i=0;i<obj.length;i++){
                if(obj[i].amount==""){
                    alert("产品物料数量不能为空")
                    return;
                }
                if(obj[i].amount<1){
                    alert("产品物料数量不能小于1")
                    return;
                }
            }

            module2["moduledetails"]=obj;

            console.log(module2)

            $.ajax({
                url:"AddModuleAndBatchModuleDetails.action",
                data:JSON.stringify(module2),
                dataType:"json",
                contentType:"application/json",
                type:"post",
                success:function (data) {
                    alert(data.msg)
                }
            })

        }

        //点击返回到制定物料组成设计单
        function return2(){
            $("#tab1").addTabs("制定物料组成设计单","","MakeMaterialList.jsp","",true);
            $("#tab1").closeTabs("物料组成设计单")
        }

        //给产品编号和产品名字赋值
        var productid='${param.productId}';
        $("#MaterialCompositionDesignSheetProductId").val(productid);
        var productname='${param.productName}';
        $("#MaterialCompositionDesignSheetProductName").val(productname);

        //明细表格数据
        $("#MaterialCompositionDesignSheetTab").bootstrapTable({
            height:200,
            uniqueId: "productid",                     //每一行的唯一标识，一般为主键列
            columns:[
                {
                    field:"",
                    title: "点选",
                    checkbox:true
                },
                {
                    field: "productid",
                    title: "物料编号"
                },
                {
                    field: "productname",
                    title: "物料名称"
                },
                {
                    field: "type",
                    title: "用途类型",
                    formatter:function (value,row,index) {
                        if(value=="Y001-2"){
                            return "物料";
                        }else{
                            return "商品";
                        }
                    }
                },
                {
                    field: "productdescribe",
                    title: "描述"
                },
                {
                    field: "amount",
                    title:"数量",
                    formatter:function (value,row,index) {
                        return "<input type='number' style='outline: none;border: none' value='"+value+"' onchange='MaterialCompositionDesignSheetChangeAmount(this)'>";
                    }
                },
                {
                    field: "amountunit",
                    title: "单位"
                },
                {
                    field: "costprice",
                    title: "计划成本单价(元)"
                },
                {
                    field: "subtotal",
                    title: "小计(元)"
                }
            ]
        })


        //删除物料重新添加
        function delMaterialCompositionDesignSheet() {

            //获取你选中的物料
            var cks=$("#MaterialCompositionDesignSheetTab").bootstrapTable("getSelections");

            if(cks.length==0){
                alert("请选择一个在删除");
                return ;
            }else{
                $(cks).each(function (index,item) {
                    $("#MaterialCompositionDesignSheetTab").bootstrapTable("removeByUniqueId",item.productid)
                })
            }

        }


        function MaterialCompositionDesignSheetShowTime() {
            //获取当前时间
            var time=new Date();
            var current=time.getFullYear()+"/"+(time.getMonth()+1)+"/"+time.getDate()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
            $("#MaterialCompositionDesignSheetRegisterTime").text(current)
        }

        //计算器
        $(function () {
            setInterval(MaterialCompositionDesignSheetShowTime(),1000)
        })


        //添加物料的方法
        function addFood() {
            $("#DesignTab_Modal").load("addProFood.jsp")
            $("#DesignTabModal").modal("show")
        }

    </script>
</body>
</html>
